/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.opmviewer.converters;

import edu.uci.ics.jung.graph.DelegateForest;
import edu.uci.ics.jung.graph.Forest;
import edu.uci.ics.jung.visualization.LayeredIcon;
import java.util.HashMap;
import java.util.Map;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import org.openprovenance.model.Artifact;
import org.openprovenance.model.Edge;
import org.openprovenance.model.OPMFactory;
import org.openprovenance.model.OPMGraph;
import org.openprovenance.model.Process;
import org.openprovenance.model.Used;
import org.openprovenance.model.WasGeneratedBy;
import org.openprovenance.model.WasTriggeredBy;
import org.opmviewer.jung.JungOPMArtifact;
import org.opmviewer.jung.JungOPMProcess;
import org.opmviewer.jung.JungOPMVertex;

/**
 *
 * @author grzegorz
 */
public class Opm2JungConverter {

    public Opm2JungConverter() {
        iconMap = new HashMap<JungOPMVertex, Icon>();
    }
    Map<JungOPMVertex, Icon> iconMap;



    public Forest<JungOPMVertex, Edge> opm2Jung(OPMGraph opmg) {
        if (opmg == null) System.out.println("NULL!");
        //System.out.println(opmg.getAnnotations().getAnnotation().size());
        Forest<JungOPMVertex, Edge> g =
                new DelegateForest<JungOPMVertex, Edge>();
        for (Object o : opmg.getDependencies().
                getUsedOrWasGeneratedByOrWasTriggeredBy()) {
            if (o instanceof Used) {

                Used u = (Used) o;
                JungOPMArtifact a = 
                        new JungOPMArtifact((Artifact) u.getCause().getRef());
                JungOPMProcess p = 
                        new JungOPMProcess((Process) u.getEffect().getRef());
                g.addEdge(u, p, a);
                addToIconMap(p);
                addToIconMap(a);

            } else if (o instanceof WasGeneratedBy) {
                WasGeneratedBy wgb = (WasGeneratedBy) o;
                JungOPMArtifact a =
                        new JungOPMArtifact((Artifact) wgb.getEffect().getRef());
                JungOPMProcess p = 
                        new JungOPMProcess((Process) wgb.getCause().getRef());
                g.addEdge(wgb, a, p);
                addToIconMap(p);
                addToIconMap(a);
            } else if (o instanceof WasTriggeredBy) {
                WasTriggeredBy wtb = (WasTriggeredBy) o;
                JungOPMProcess p1 = 
                        new JungOPMProcess((Process) wtb.getCause().getRef());
                JungOPMProcess p2 = 
                        new JungOPMProcess((Process) wtb.getEffect().getRef());
                g.addEdge(wtb, p2, p1);
                addToIconMap(p1);
                addToIconMap(p2);
            }


        }
        return g;
    }

    public Forest<JungOPMVertex, Edge> opm2Jung(String fileName) {
        OPMFactory of = new OPMFactory();
       
        OPMGraph opmg = null;
        OPMRdf2Xml rdf2xml = new OPMRdf2Xml();
        try {
            opmg = rdf2xml.convert(fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return opm2Jung(opmg);
    }

    private void addToIconMap(JungOPMProcess p){
            Icon pi;
            try {
                pi = new LayeredIcon(
                        (new ImageIcon(getClass().
                        getResource("/ProcessIcon.png"))).getImage());
            } catch(Exception ex) {
                System.err.println("Image not found!: ProcessIcon.png");
                return;
            }
            iconMap.put(p, pi);
    }
    private void addToIconMap(JungOPMArtifact a){
            Icon ai;
            try {
                ai = new LayeredIcon(
                        (new ImageIcon(getClass().
                        getResource("/ArtifactIcon.png"))).getImage());
            } catch(Exception ex) {
                ex.printStackTrace();
                System.err.println("Image not found!: ArtifactIcon.png");
                return;
            }
            iconMap.put(a, ai);
    }
    public Map<JungOPMVertex, Icon> getIconMap(){
        return iconMap;
    }
}
